package com.my.behaviourtype.template.jdbctemplate;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestMyJdbcTemplate {

	/** 
	 *  方法的简述. 
	 * <p> 方法的详细说明第一行<br> 
	 *  方法的详细说明第二行 
	 * @param 参数
	 * @exception 异常描述
	 * @return 没有返回值 
	 */

	public static void main(String[] args) {
		MyJdbcTemplate template = new MyJdbcTemplate();
		template.setClassName("com.mysql.jdbc.Driver");
		template.setUrl("jdbc:mysql://localhost:3306/library");
		template.setUser("root");
		template.setPassword("root");
		final String sql = "insert into user(name, password) values('a', 'a')";
		try {
			int ret = template.execute(new StatementCallback<Integer>() {

				@Override
				public Integer doInStatement(Statement stmt) throws SQLException {
					int ret = stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
					ResultSet rs = stmt.getGeneratedKeys();
					while(rs.next()) {
						System.out.println("generated key: " + rs.getInt(1));
					}
					return ret;
				}
				
			});
			
			System.out.println("rows of changed: " + ret);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
